package william.xor;

/**
 * @author ZhangShenao
 * @date 2024/1/19
 * @description <a href="https://leetcode.cn/problems/swap-numbers-lcci/description/">...</a>
 */
public class Leetcode_面试题16_01_交换数字 {
    /**
     * 利用异或操作(^)的性质
     * N^N=0
     * 0^N=N
     */
    public int[] swapNumbers(int[] numbers) {
        //边界条件校验
        if (numbers == null || numbers.length != 2) {
            return new int[0];
        }

        //采用异或操作
        numbers[0] = numbers[0] ^ numbers[1];
        numbers[1] = numbers[0] ^ numbers[1];
        numbers[0] = numbers[0] ^ numbers[1];

        //返回结果
        return numbers;
    }
}
